package main

import (
	"bufio"
	"fmt"
	"github.com/vmihailenco/msgpack/v5"
	"learn001/analyti"
	"learn001/server"
	"os"
	"time"
)

type CliOptions interface {
	// AddFlags adds flags to the specified FlagSet object.
	// AddFlags(fs *pflag.FlagSet)
	Flags() (fss string)
	Validate() error
}

func main() {

	store := analyti.FakeStore{}
	newAnalytics := analyti.NewAnalytics(&store)
	go listenForInput()

	stopCh := server.SetupSignalHandler()

	go server.Run(stopCh)

	newAnalytics.Start()

	//ExampleMarshal()
	//// 创建根命令
	//var rootCmd = &cobra.Command{Use: "myapp", Short: "My example Cobra app"}
	//
	//// 创建子命令
	//var helloCmd = &cobra.Command{
	//	Use:   "hello",
	//	Short: "Prints 'Hello, World!'",
	//	Run: func(cmd *cobra.Command, args []string) {
	//		fmt.Println("Hello, World!")
	//	},
	//}
	//
	//// 将子命令添加到根命令
	//rootCmd.AddCommand(helloCmd)
	//
	//// 执行应用
	//if err := rootCmd.Execute(); err != nil {
	//	fmt.Println(err)
	//	os.Exit(1)
	//}

	// 创建根命令
	//var rootCmd = &cobra.Command{
	//	Use:   "myapp",
	//	Short: "My simple Cobra app",
	//	Run: func(cmd *cobra.Command, args []string) {
	//		// 在这里放置您的根命令逻辑
	//		fmt.Println("Hello from myapp!")
	//	},
	//}
	//
	//// 执行应用
	//if err := rootCmd.Execute(); err != nil {
	//	fmt.Println(err)
	//	os.Exit(1)
	//}
}

func test(c CliOptions) {
	fmt.Println(c.Flags())
}

func ExampleMarshal() {
	type Item struct {
		Foo string
	}

	b, err := msgpack.Marshal(&Item{Foo: "bar"})
	if err != nil {
		panic(err)
	}

	var item Item
	err = msgpack.Unmarshal(b, &item)
	if err != nil {
		panic(err)
	}
	fmt.Println(item.Foo)
	// Output: bar
}

func listenForInput() {
	scanner := bufio.NewScanner(os.Stdin)
	for {
		fmt.Print("Enter username: ")
		scanner.Scan()

		// 获取用户输入的内容
		username := scanner.Text()

		// 构造 AnalyticsRecord
		record := &analyti.AnalyticsRecord{
			TimeStamp: time.Now().Unix(),
			Username:  username,
		}

		// 调用 RecordHit 进行测试
		err := analyti.GetAnalytics().RecordHit(record)
		if err != nil {
			fmt.Printf("Error recording hit: %s\n", err)
		} else {
			fmt.Println("Hit recorded successfully!")
		}
	}
}
